#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=1e5+1000;
typedef long long LL;
inline LL read(){LL x=0,f=1;char ch=getchar();	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
vector<LL>g[maxn];
LL dp[maxn][2];
struct P{
    LL l,r;
}a[maxn];
void dfs(LL u,LL fa){
     dp[u][0]=0;
     dp[u][1]=0;

     for(LL i=0;i<g[u].size();i++){
         LL v=g[u][i];
         if(v==fa) continue;
         dfs(v,u);
         LL tmp1=dp[v][0]+abs(a[u].l-a[v].l);
         LL tmp2=dp[v][1]+abs(a[u].l-a[v].r);
         dp[u][0]+=max(tmp1,tmp2);
         LL tmp3=dp[v][0]+abs(a[u].r-a[v].l);
         LL tmp4=dp[v][1]+abs(a[u].r-a[v].r);
         dp[u][1]+=max(tmp3,tmp4);
     }

}
int main(void){
    LL t;t=read();
    while(t--){
        LL n;n=read();
        for(LL i=0;i<n+10;i++) g[i].clear(),dp[i][0]=dp[i][1]=0;
        for(LL i=1;i<=n;i++){
            a[i].l=read();a[i].r=read();
        }
        for(LL i=1;i<n;i++){
            LL u,v;u=read();v=read();
            g[u].push_back(v);
            g[v].push_back(u);
        }
        dfs(1,0);
        cout<<max(dp[1][0],dp[1][1])<<"\n";
    }
   	return 0;
}
